﻿<html>

<head>
<script type="text/javascript">
	function confirmar(){
		if(!confirm("Processo realizado com sucesso\n" +
					"Deseja verificar a alteração?")){
			window.location = 'index.php';
		}
	}
</script>
<head>

<?php

    $titulo; 
	$usuario; 
	$conteudo; 
	$dtPost;
	
	if(isset($_GET["id"])){
		$id        = $_GET["id"];
		$xml       = new SimpleXMLElement('../blog.xml', null, true);
		$result    = $xml->xpath('//post[@idPost ='.$id.' ]');
		$titulo    = $result[0]->titulo;
		$usuario   = $result[0]["idUsuario"];
		$conteudo  = $result[0]->conteudo; 
		$dtPost    = $result[0]->data;
	}
	
	if($_POST){
		$id 	  = $_POST["txtId"]; 
		$titulo   = $_POST["txtTitulo"]; 
		$usuario  = $_POST["txtUsuario"];
		$conteudo = $_POST["txtConteudo"];
		
		$xml = new DomDocument();
		$xml->load('../blog.xml');
		$xml->formatOutput = true;
		
		$posts = $xml->getElementsByTagName('post');
		
		foreach($posts as $post){
			$idPost = $post->getAttribute('idPost');
			if($idPost == $id){
				$post->getElementsByTagName('titulo')->item(0)->nodeValue = $titulo;
				$post->getElementsByTagName('conteudo')->item(0)->nodeValue = $conteudo;
			    if($xml->save('../blog.xml')){
					echo "<script> confirmar(); </script>";
				}
			}
		}
	}
?>

<body>
	<form action="detalhePost.php" method="post">
		Titulo:
		<br />
		<input type="text" name="txtTitulo" id="txtTitulo" size="63" value="<?php echo $titulo; ?>" />
		<br />
		<br />
		Usuario:
		<br />
		<input type="text" name="txtUsuario" id="txtUsuario" value="<?php echo $usuario ?>" />
		<br />
		<br />
		Conteúdo:
		<br />
		<textarea rows="20" cols="100" name="txtConteudo" id="txtConteudo"><?php echo $conteudo ?></textarea>
		<br />
		<input type="submit" value="Alterar" id="btnAlterar" />
		<input type="hidden" name="txtId" id="txtId" value="<?php echo $id; ?>" />
    </form>		
		<a href="listarPost.php">Voltar</a>
<body>
</html>